// 导入axios
import axios from "axios";
// 定义host
const host = `http://localhost:4000`
// 封装GET请求
const getFn = (url:string,data={}) => {
    return axios.get(
        `${host}${url}`,
        {
            params: data,
            headers: {
                token: localStorage.getItem("AUTOTOKEN")
            }
        },
    )
    .then(
         response=> response.data 
    )
    .catch(
        error=> {
            console.log("请求失败..",error)
        }
    )
}
// 封装POST请求
const postFn = (url:string,data={}) => {
    return axios.post(
        `${host}${url}`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                "token": localStorage.getItem("AUTOTOKEN")
            }
        }
    )
    .then(
         response=> response.data 
    )
    .catch(
        error=> {
            console.log("请求失败..",error)
        }
    )
}
const uploadFn = (url:string, data={}) => {
    return axios.post(
        `${host}${url}`,
         data,
         {
            headers: {
                'Content-Type': 'multipart/form-data',
                 token: localStorage.getItem("TOKEN")
            }
        }
    )
    .then(response => response.data)
}

export default {
    host,
    getFn,
    postFn,
    uploadFn
}

